#include "voicecontext.h"

WasapiContext::WasapiContext()
{
	m_vFormatParam.resize(devicesNum);
	m_vSuccess.resize(devicesNum, true);
	m_pSwrConvert = NULL;
}

WasapiContext::~WasapiContext()
{
	SAFE_FREE_ARRAY(m_pSwrConvert);
}

int WasapiContext::GetSize(ctxType type)
{
	switch (type)
	{
	case VOICE:
		return m_voiceinfo.size();
		break;
	case FORMAT:
		return m_vFormatParam.size();
		break;
	case SUCCESS:
		return m_vSuccess.size();
		break;
	default:
		break;
	}
}

void WasapiContext::Resize(ctxType type, int size)
{
	switch (type)
	{
	case VOICE:
		m_voiceinfo.resize(size);
		break;
	case FORMAT:
		m_vFormatParam.resize(size);
		break;
	case SUCCESS:
		m_vSuccess.resize(size, true);
		break;
	default:
		break;
	}
}

void WasapiContext::PushInfo(void* pInfo, ctxType type, int i)
{
	if (pInfo)
	{
		switch (type)
		{
		case VOICE:
			i < 0 ? m_voiceinfo.push_back(*((VoiceInfo*)pInfo)) : m_voiceinfo[i] = *((VoiceInfo*)pInfo);
			break;
		case FORMAT:
			i < 0 ? m_vFormatParam.push_back(*((FormatParam*)pInfo)) : m_vFormatParam[i] = *((FormatParam*)pInfo);
			break;
		case SUCCESS:
			i < 0 ? m_vSuccess.push_back(*((bool*)pInfo)) : m_vSuccess[i] = *((bool*)pInfo);
			break;
		default:
			break;
		}
	}
}

void* WasapiContext::PullInfo(ctxType type, int i)
{
	bool b;
	switch (type)
	{
	case VOICE:
		return (void*)(&m_voiceinfo[i]);
		break;
	case FORMAT:
		return (void*)(&m_vFormatParam[i]);
		break;
	case SUCCESS:
		b = m_vSuccess[i];
		return b ? &b : NULL;
		break;
	case MIX:
		return (void*)(&m_mixedVoice);
		break;
	default:
		break;
	}
	return NULL;
}

WasapiContext* g_wasapi_ctx = new WasapiContext;

VoiceData* GetMixInfo()
{
	return (VoiceData*)g_wasapi_ctx->PullInfo(MIX, -1);
}

VoiceInfo* GetVoiceInfo(int i)
{
	return (VoiceInfo*)g_wasapi_ctx->PullInfo(VOICE, i);
}

FormatParam* GetFormatInfo(int i)
{
	return (FormatParam*)g_wasapi_ctx->PullInfo(FORMAT, i);
}

bool GetSuccessInfo(int i)
{
	void* p = g_wasapi_ctx->PullInfo(SUCCESS, i);
	return p ? true : false;
}

void SetInfo(void* pInfo, ctxType type, int i)
{
	g_wasapi_ctx->PushInfo(pInfo, type, i);
}

void InitSwrConvert(int size)
{
	if (g_wasapi_ctx->m_pSwrConvert == NULL)
	{
		g_wasapi_ctx->m_pSwrConvert = new SwrConvert[size];
		for (int i = 0; i < size; i++)
		{
			g_wasapi_ctx->m_pSwrConvert[i].SetParam(
				GetFormatInfo(i)->m_captureFormat,
				GetFormatInfo(i)->m_outFormat,
				GetFormatInfo(i)->m_captureChannel,
				GetFormatInfo(i)->m_outChannel,
				GetFormatInfo(i)->m_captureSamples,
				GetFormatInfo(i)->m_outSamples);
		}
	}
}

SwrConvert* GetSwrConvert(int i)
{
	return &g_wasapi_ctx->m_pSwrConvert[i];
}
